<?php

namespace app\common\service\organization;

use app\common\model\banxi\organization\OrganizationQuotaApply;
use app\common\model\banxi\organization\OrganizationQuotaRw;
use app\common\service\ApiService;
use app\common\service\ConfigService;
use ba\Exception;
use think\db\exception\PDOException;
use think\exception\ValidateException;
use think\facade\Db;

class OrganizationQuotaService extends ApiService
{
    /**
     * 审核
     */
    public function applyAudit(array $data)
    {
        Db::startTrans();
        try {
            $applyId = $data['applyId'];
            $organizationQuotaApply = new OrganizationQuotaApply();
            $quotaApply = $organizationQuotaApply->where('id', $applyId)->find();
            if (empty($quotaApply)) {
                throw new ValidateException('申请记录不存在');
            }
            $status = $quotaApply['status'];
            if ($status != 1) {
                throw new ValidateException('该申请已被审核');
            }
            $result = $data['result'];
            $updateApply = [];
            if ($result) {
                $updateApply['status'] = 2;
            } else {
                $updateApply['status'] = 3;
                $updateApply['rejectReason'] = $data['rejectReason'];
            }
            $cnt = $organizationQuotaApply->update($updateApply, ['id' => $applyId, 'status' => $status]);
            if (!$cnt) {
                throw new ValidateException('审核失败，请稍后重试');
            }
            if ($result) {
                $organizationQuotaRw = new OrganizationQuotaRw();
                $trade = [
                    'describe' => '名额申请',
                    'organizationId' => $quotaApply['organizationId'],
                    'value' => $quotaApply['value'],
                    'businessType' => 1,
                    'businessId' => $applyId
                ];
                $organizationQuotaRw->updateAccount($trade);
            }
            Db::commit();
        } catch (Exception|PDOException|ValidateException $e) {
            Db::rollback();
            $this->setError($e->getMessage());
            return false;
        }
        return true;
    }

    /**
     * 名额申请
     */
    public function applyQuota(string $organizationId, array $apply)
    {
        try {
            $minValue = (new ConfigService)->getSysConfigValue('min_buy_organization_num');
            $value = $apply['value'];
            if ($minValue > $value) {
                throw new ValidateException('申请失败，最低申请数量为：' . $minValue);
            }
            $quotaApply = [
                'organizationId' => $organizationId,
                'value' => $apply['value'],
                'status' => 1
            ];
            $organizationQuotaApply = new OrganizationQuotaApply();
            $organizationQuotaApply->save($quotaApply);
        } catch (Exception|PDOException|ValidateException $e) {
            $this->setError($e->getMessage());
            return false;
        }
        return true;
    }
}
